﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class ContactController : Controller
    {
        //
        // GET: /Administrator/Contact/
        CollegeWebsiteDataContext _dataContext = new CollegeWebsiteDataContext();
        public ActionResult Index()
        {
            List<Contact> contact = _dataContext.Contacts.Select(c => c).ToList();
            return View(contact);
        }

        //
        // GET: /Administrator/Contact/Details/5

        public ActionResult Details(int id)
        {
            Contact contact = _dataContext.Contacts.Single(c => c.ContactID == id);
            return View(contact);
        }

        //
        // GET: /Administrator/Contact/Create

        public ActionResult Create()
        {
            Contact contact = new Contact();
            contact.ContactStatus = true;
            return View(contact);
        } 

        //
        // POST: /Administrator/Contact/Create

        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // TODO: Add insert logic here
                    _dataContext.Contacts.InsertOnSubmit(contact);
                    _dataContext.SubmitChanges();
                    return RedirectToAction("Index");    
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Administrator/Contact/Edit/5
 
        public ActionResult Edit(int id)
        {
            Contact contact = _dataContext.Contacts.Single(c => c.ContactID == id);
            return View(contact);
        }

        //
        // POST: /Administrator/Contact/Edit/5

        [HttpPost]
        public ActionResult Edit(int id,Contact newcontact)
        {
            try
            {
                // TODO: Add update logic here
                Contact contact = _dataContext.Contacts.Single(c => c.ContactID == id);

                contact.ContactStatus = newcontact.ContactStatus;

                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administrator/Contact/Delete/5
 
        public ActionResult Delete(int id)
        {
            Contact contact = _dataContext.Contacts.Single(c => c.ContactID == id);
            return View(contact);
        }

        //
        // POST: /Administrator/Contact/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Contact contact = _dataContext.Contacts.Single(c => c.ContactID == id);
                _dataContext.Contacts.DeleteOnSubmit(contact);
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
